//
//  UIImage+SYAdd.m
//  Image-Decompression-Benchmark
//
//  Created by huchu on 2018/7/17.
//  Copyright © 2018年 leichunfeng. All rights reserved.
//

#import "UIImage+SYNAdd.h"
#import "UIImage+YYAdd.h"
#import "YYCGUtilities.h"
@implementation UIImage (SYNAdd)
- (NSData *)compressByQualityToMaxLength:(NSInteger)maxLength {
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(self, compression);
    while (data.length > maxLength && compression > 0) {
        compression -= 0.02;
        data = UIImageJPEGRepresentation(self, compression); // When compression less than a value, this code dose not work
    }
    return data;
}
- (NSData *)compressByQualityBisectionToMaxLength:(NSInteger)maxLength {
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(self, compression);
    if (data.length < maxLength) return data;
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(self, compression);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    return data;
}
-(NSData *)compressBySizeToMaxLength:(NSUInteger)maxLength {
    UIImage *resultImage = self;
    NSData *data = UIImageJPEGRepresentation(resultImage, 1);
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        // Use image to draw (drawInRect:), image is larger but more compression time
        // Use result image to draw, image is smaller but less compression time
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, 1);
    }
    return data;
}
-(NSData *)compressByQualityAndSizeToMaxLength:(NSUInteger)maxLength {
    // Compress by quality
    CGFloat compression = 1;
    NSData *data = UIImageJPEGRepresentation(self, compression);
    //NSLog(@"Before compressing quality, image size = %ld KB",data.length/1024);
    if (data.length < maxLength) return data;
    
    CGFloat max = 1;
    CGFloat min = 0;
    for (int i = 0; i < 6; ++i) {
        compression = (max + min) / 2;
        data = UIImageJPEGRepresentation(self, compression);
        //NSLog(@"Compression = %.1f", compression);
        //NSLog(@"In compressing quality loop, image size = %ld KB", data.length / 1024);
        if (data.length < maxLength * 0.9) {
            min = compression;
        } else if (data.length > maxLength) {
            max = compression;
        } else {
            break;
        }
    }
    //NSLog(@"After compressing quality, image size = %ld KB", data.length / 1024);
    if (data.length < maxLength) return data;
    UIImage *resultImage = [UIImage imageWithData:data];
    // Compress by size
    NSUInteger lastDataLength = 0;
    while (data.length > maxLength && data.length != lastDataLength) {
        lastDataLength = data.length;
        CGFloat ratio = (CGFloat)maxLength / data.length;
        //NSLog(@"Ratio = %.1f", ratio);
        CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
                                 (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
        UIGraphicsBeginImageContext(size);
        [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
        resultImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        data = UIImageJPEGRepresentation(resultImage, compression);
        //NSLog(@"In compressing size loop, image size = %ld KB", data.length / 1024);
    }
    //NSLog(@"After compressing size loop, image size = %ld KB", data.length / 1024);
    return data;
}
- (NSData *)compressByFixedSize:(float)size {
    UIImage *compressedImage= [self _compressImageToSize:size];
    NSData *data = UIImageJPEGRepresentation(compressedImage, 0.9);
    return data;
}
-(NSData *)compressByFixedQuality {
    NSData *data=UIImageJPEGRepresentation(self, 0.9);
    //大于100k压缩
    if (data.length>100*1024) {
        if (data.length>1024*1024) {//1M以及以上
            data=UIImageJPEGRepresentation(self, 0.7);
        }else if (data.length>512*1024) {//0.5M-1M
            data=UIImageJPEGRepresentation(self, 0.8);
        }else if (data.length>200*1024) {
            //0.25M-0.5M
            data=UIImageJPEGRepresentation(self, 0.9);
        }
    }
    return data;
}
-(UIImage *)_compressImageToSize:(float)size{
    //    CGSize imageSize = self.size;//取出要压缩的image尺寸
    //    CGFloat width = imageSize.width;    //图片宽度
    //    CGFloat height = imageSize.height;  //图片高度
    //
    //    if (width>size||height>size) {
    //        if (width>height) {
    //            CGFloat scale = height/width;
    //            width = size;
    //            height = width*scale;
    //        }else{
    //            CGFloat scale = width/height;
    //            height = size;
    //            width = height*scale;
    //        }
    //
    //    }else if(width>size||height<size){
    //        CGFloat scale = height/width;
    //        width = size;
    //        height = width*scale;
    //
    //    }else if(width<size||height>size){
    //        CGFloat scale = width/height;
    //        height = size;
    //        width = height*scale;
    //
    //    }else{
    //    }
    //    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    //    [self drawInRect:CGRectMake(0,0,width,height)];
    //    UIImage* compressedImage = UIGraphicsGetImageFromCurrentImageContext();
    //    UIGraphicsEndImageContext();
    
    CGRect aspectRect=  YYCGRectFitWithContentMode( CGRectMake(0, 0, size , size), self.size, UIViewContentModeScaleAspectFit);
    UIImage* compressedImage = [self  imageByResizeToSize:aspectRect.size];
    return compressedImage;
}
-(NSData *)compressByFixedSizeAndQuality:(float)size {
    UIImage *image=  [self _compressImageToSize:size];
    NSData *data= [image compressByFixedQuality];
    return data;
}
@end



